package cn.wangchao.org.controller;

import cn.wangchao.basic.exception.BusinessException;
import cn.wangchao.basic.util.ExcelUtil;
import cn.wangchao.org.domain.ShopAuditLog;
import cn.wangchao.org.service.IShopAuditLogService;
import cn.wangchao.org.service.IShopService;
import cn.wangchao.org.domain.Shop;
import cn.wangchao.org.query.ShopQuery;
import cn.wangchao.basic.util.AjaxResult;
import cn.wangchao.basic.util.PageList;
import cn.wangchao.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;
    @Autowired
    public IShopAuditLogService shopAuditLogService;

    /**
     * 报表展示
     * @return
     */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        Map<String, Object> map = shopService.queryEcharts();
        return map;
    }

    /**
     * 激活
     * @param id
     * @param response
     */
    @GetMapping("/activation/{id}")
    public void activation(@PathVariable("id") Long id,HttpServletResponse response){
        try {
            shopService.activation(id);
            response.sendRedirect("http://localhost:8081/#/login");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops,null,"店铺信息", Shop.class,"shop.xlxs",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @PostMapping("/importExcel")
    public void importExcel(@RequestBody MultipartFile file){
        try {
            List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
            shopService.batchSave(shops);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 审核通过
     * @param log
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return AjaxResult.my();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 审核驳回
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return AjaxResult.my();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.my();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.my();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }
}
